//
//  SampuxMinPickerView.m
//  Sampux
//
//  Created by rzm on 16/9/11.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxMinPickerView.h"


@interface SampuxMinPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, assign) NSInteger selectedIndex ;

@end
@implementation SampuxMinPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createMinPickerView];
    }
    return self;
    
}

- (void)createMinPickerView
{
//    _minPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0,0,  self.width ,self.height)];
    self.delegate = self;
    self.dataSource = self;
//    [self addSubview:_minPicker];
    [self setValue:THWhiteAlplaColor(0.8) forKey:@"textColor"];
    
}


#pragma mark UIPickerViewDetaSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return MinArr.count *50;
}

#pragma mark UIPickerViewDelegate
/**
 *  pickerView每行的视图
 */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumScaleFactor = 12.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:24]];
        pickerLabel.textColor = THWhiteAlplaColor(0.8);
        
    }
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = THWhiteAlplaColor(0.8);
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = THWhiteAlplaColor(0.8);
    // Fill the label text here
    pickerLabel.text = [MinArr objectAtIndex:(row%[MinArr count])];
    return pickerLabel;
}

/**
 *  轮子显示内容
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [MinArr objectAtIndex:(row%[MinArr count])];
}

/**
 *  轮子的移动
 // */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSUInteger max = 0;
    NSUInteger base10 = 0;
    if(component == 0)
    {
        //        max = [TempArr count]*50;
        //        base10 = (max/2)-(max/2)%[TempArr count];
        [pickerView selectRow:[pickerView selectedRowInComponent:component]%[MinArr count]+base10 inComponent:component animated:false];
    }
    self.selectedIndex = [pickerView selectedRowInComponent:0]%[MinArr count];
    self.minStr = MinArr[self.selectedIndex];
    
    if (self.installDelegate && [self.installDelegate respondsToSelector:@selector(changeMin:)]) {
        [self.installDelegate changeMin:self.minStr];
    }
}

/**
 *  pickerView的行高
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30.;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    
}


-(void)setMinStr:(NSString *)minStr
{
    _minStr = minStr;
}

- (void)setPickerInitialRow:(NSInteger)pickerInitialRow
{
    _pickerInitialRow = pickerInitialRow;
    [self selectRow:pickerInitialRow inComponent:0 animated:YES];
    
}



@end
